package day9;

import java.io.*;
import java.net.Socket;

public class ChatThread extends Thread{
    private final Socket s;

    public ChatThread(Socket s){
        this.s=s;
    }

    @Override
    public void run() {
        System.out.println("ip:" + s.getInetAddress());
        System.out.println("远程端口：" + s.getPort());
        System.out.println(s.getLocalPort() + "本地");
        System.out.println("socket连接建立了。。。" + s);
        try {
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            //客户端回，服务器答，
            //BufferedWriter pw=new BufferedWriter(out,true);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            PrintWriter pw = new PrintWriter(out, true);
            while (true){
                String str = br.readLine();
                System.out.println("客户端问：" + str);
                pw.println("你问的:" + str + ",朕知道了");
                if (str == null || "null".equals(str) || "byebye".equals(str))
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
                System.out.println(s+"关闭了。。。聊天结束");
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}
